﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Physis.Client.Controllers {

    public class InsurerController : ApplicationController {

        #region Index/Workspace

        public ActionResult Index() {

            return View ("~/Views/Insurer/InsurerWorkspace.cshtml", applicationModel);

        }

        public ActionResult Workspace () { return Index (); }

        #endregion 


        #region Grid Queries

        public JsonResult InsurersByInsurerTypeForGrid (Physis.Core.Insurer.Enumerations.InsurerType insurerType, Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Insurer.Insurer> insurers = applicationModel.Application.Core.Insurer.Insurers (useCaching);

            if (applicationModel.Application.LastException == null) {

                insurers = insurers.Where (current => current.InsurerType == insurerType).ToList ();

                insurers = insurers.OrderBy (current => current.Name).ToList ();

                response.RowsByPage (insurers);

            }

            else {

                response.SetException (applicationModel.Application.LastException);

            }
            
            return Json (response, JsonRequestBehavior.AllowGet);

        }

        public JsonResult ProgramTypesForGrid (Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Insurer.ProgramType> programTypes = applicationModel.Application.Core.Insurer.ProgramTypes ();

            response.RowsByPage (programTypes);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        public JsonResult ProgramsForGrid (Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Insurer.Program> programs = applicationModel.Application.Core.Insurer.Programs();

            response.RowsByPage (programs);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        public JsonResult InsuranceTypesForGrid (Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Insurer.InsuranceType> insuranceTypes = applicationModel.Application.Core.Insurer.InsuranceTypes ();

            response.RowsByPage (insuranceTypes);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        public JsonResult BenefitPlansForGrid (Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Insurer.BenefitPlan> benefitPlans = applicationModel.Application.Core.Insurer.BenefitPlans ();

            response.RowsByPage (benefitPlans);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        public JsonResult RidersForGrid (Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Insurer.Rider> riders = applicationModel.Application.Core.Insurer.Riders ();

            response.RowsByPage (riders);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        public JsonResult ProductsForGrid (Boolean useCaching = true) {

            JsonResponses.jqGridDataResponse response = new JsonResponses.jqGridDataResponse (Request.QueryString);

            List<Physis.Core.Insurer.Product> products = applicationModel.Application.Core.Insurer.Products ();

            response.RowsByPage (products);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        #endregion 


        #region Insurer

        [HttpGet]
        public ActionResult Insurer (Int64 id) {

            Models.Insurer.InsurerModel model = new Models.Insurer.InsurerModel (id);

            return View ("~/Views/Insurer/Insurer.cshtml", model);

        }

        [HttpPost]
        public JsonResult InsurerSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Insurer.InsurerModel model = new Models.Insurer.InsurerModel (Request.Form);

            response.Result = model.Insurer.Save ();

            response.Id = model.Insurer.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult InsurerDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Insurer.InsurerModel model = new Models.Insurer.InsurerModel (id);

            response.Result = model.Insurer.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 


        #region Program Types

        [HttpGet]
        public JsonResult ProgramTypes (Boolean useCaching = true) {

            JsonResponses.ObjectCollectionResponse response = new JsonResponses.ObjectCollectionResponse ();

            response.Objects = applicationModel.Application.Core.Insurer.ProgramTypes (useCaching);

            response.SetException (applicationModel.Application.LastException);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        [HttpGet]
        public ActionResult ProgramType (Int64 id) {

            Models.Insurer.ProgramTypeModel model = new Models.Insurer.ProgramTypeModel (id);

            return View ("~/Views/Insurer/ProgramType.cshtml", model);

        }

        [HttpPost]
        public JsonResult ProgramTypeSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Insurer.ProgramTypeModel model = new Models.Insurer.ProgramTypeModel (Request.Form);

            response.Result = model.ProgramType.Save ();

            response.Id = model.ProgramType.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult ProgramTypeDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Insurer.ProgramTypeModel model = new Models.Insurer.ProgramTypeModel (id);

            response.Result = model.ProgramType.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 
        

        #region Programs

        [HttpGet]
        public ActionResult Program (Int64 id) {

            Models.Insurer.ProgramModel model = new Models.Insurer.ProgramModel (id);

            return View ("~/Views/Insurer/Program.cshtml", model);

        }

        [HttpPost]
        public JsonResult ProgramSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Insurer.ProgramModel model = new Models.Insurer.ProgramModel (Request.Form);

            response.Result = model.Program.Save ();

            response.Id = model.Program.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult ProgramDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Insurer.ProgramModel model = new Models.Insurer.ProgramModel (id);

            response.Result = model.Program.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 

        
        #region Insurance Types

        [HttpGet]
        public JsonResult InsuranceTypes (Boolean useCaching = true) {

            JsonResponses.ObjectCollectionResponse response = new JsonResponses.ObjectCollectionResponse ();

            response.Objects = applicationModel.Application.Core.Insurer.InsuranceTypes (useCaching);

            response.SetException (applicationModel.Application.LastException);

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        [HttpGet]
        public ActionResult InsuranceType (Int64 id) {

            Models.Insurer.InsuranceTypeModel model = new Models.Insurer.InsuranceTypeModel (id);

            return View ("~/Views/Insurer/InsuranceType.cshtml", model);

        }

        [HttpPost]
        public JsonResult InsuranceTypeSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Insurer.InsuranceTypeModel model = new Models.Insurer.InsuranceTypeModel (Request.Form);

            response.Result = model.InsuranceType.Save ();

            response.Id = model.InsuranceType.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult InsuranceTypeDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Insurer.InsuranceTypeModel model = new Models.Insurer.InsuranceTypeModel (id);

            response.Result = model.InsuranceType.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 
        

        #region BenefitPlans

        [HttpGet]
        public ActionResult BenefitPlan (Int64 id) {

            Models.Insurer.BenefitPlanModel model = new Models.Insurer.BenefitPlanModel (id);

            return View ("~/Views/Insurer/BenefitPlan.cshtml", model);

        }

        [HttpPost]
        public JsonResult BenefitPlanSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Insurer.BenefitPlanModel model = new Models.Insurer.BenefitPlanModel (Request.Form);

            response.Result = model.BenefitPlan.Save ();

            response.Id = model.BenefitPlan.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult BenefitPlanDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Insurer.BenefitPlanModel model = new Models.Insurer.BenefitPlanModel (id);

            response.Result = model.BenefitPlan.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpGet]
        public JsonResult BenefitPlansByProgramDictionary (Int64 id, Boolean includeDisabled = false, Boolean useCaching = true) {

            JsonResponses.ObjectCollectionResponse response = new JsonResponses.ObjectCollectionResponse ();
            
            response.Objects = (from currentBenefitPlan in applicationModel.Application.Core.Insurer.BenefitPlansByProgram  (id, useCaching)

                                where ((currentBenefitPlan.Enabled) || (includeDisabled))

                                select new { Id = currentBenefitPlan.Id, Name = currentBenefitPlan.Name }).ToArray ();

            return Json (response, JsonRequestBehavior.AllowGet);

        }

        #endregion 
        

        #region Riders

        [HttpGet]
        public ActionResult Rider (Int64 id) {

            Models.Insurer.RiderModel model = new Models.Insurer.RiderModel (id);

            return View ("~/Views/Insurer/Rider.cshtml", model);

        }

        [HttpPost]
        public JsonResult RiderSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Insurer.RiderModel model = new Models.Insurer.RiderModel (Request.Form);

            response.Result = model.Rider.Save ();

            response.Id = model.Rider.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult RiderDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Insurer.RiderModel model = new Models.Insurer.RiderModel (id);

            response.Result = model.Rider.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 
        

        #region Products

        [HttpGet]
        public ActionResult Product (Int64 id) {

            Models.Insurer.ProductModel model = new Models.Insurer.ProductModel (id);

            return View ("~/Views/Insurer/Product.cshtml", model);

        }

        [HttpPost]
        public JsonResult ProductSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Insurer.ProductModel model = new Models.Insurer.ProductModel (Request.Form);

            response.Result = model.Product.Save ();

            response.Id = model.Product.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult ProductDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Insurer.ProductModel model = new Models.Insurer.ProductModel (id);

            response.Result = model.Product.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 

    }

}
